package com.ruoyi.mall.controller;

import java.util.List;

import com.ruoyi.mall.entity.MallCategory;
import com.ruoyi.mall.service.IMallCategoryService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.mall.entity.MallProperty;
import com.ruoyi.mall.service.IMallPropertyService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 商品属性Controller
 * 
 * @author ruoyi
 * @date 2020-05-20
 */
@Controller
@RequestMapping("/mall/property")
public class MallPropertyController extends BaseController
{
    private String prefix = "mall/property";

    @Autowired
    private IMallPropertyService mallPropertyService;
    @Autowired
    private IMallCategoryService mallGoodCategoryService;

    @RequiresPermissions("mall:property:view")
    @GetMapping()
    public String property(ModelMap mmap)
    {
        mmap.addAttribute("categorys",getCategory());
        return prefix + "/property";
    }

    /**
     * 查询商品属性列表
     */
    @RequiresPermissions("mall:property:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(MallProperty mallProperty)
    {
        startPage();
        List<MallProperty> list = mallPropertyService.selectMallPropertyList(mallProperty);
        return getDataTable(list);
    }

    /**
     * 导出商品属性列表
     */
    @RequiresPermissions("mall:property:export")
    @Log(title = "商品属性", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(MallProperty mallProperty)
    {
        List<MallProperty> list = mallPropertyService.selectMallPropertyList(mallProperty);
        ExcelUtil<MallProperty> util = new ExcelUtil<MallProperty>(MallProperty.class);
        return util.exportExcel(list, "property");
    }
    public List<MallCategory> getCategory(){
        return mallGoodCategoryService.selectMallCategoryList(null);
    }

    /**
     * 新增商品属性
     */
    @GetMapping("/add")
    public String add(ModelMap mmap)
    {
        mmap.addAttribute("categorys",getCategory());
        return prefix + "/add";
    }

    /**
     * 新增保存商品属性
     */
    @RequiresPermissions("mall:property:add")
    @Log(title = "商品属性", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(MallProperty mallProperty)
    {
        return toAjax(mallPropertyService.insertMallProperty(mallProperty));
    }

    /**
     * 修改商品属性
     */
    @GetMapping("/edit/{propertyId}")
    public String edit(@PathVariable("propertyId") Long propertyId, ModelMap mmap)
    {
        MallProperty mallProperty = mallPropertyService.selectMallPropertyById(propertyId);
        mmap.put("mallProperty", mallProperty);
        mmap.addAttribute("categorys",getCategory());
        return prefix + "/edit";
    }

    /**
     * 修改保存商品属性
     */
    @RequiresPermissions("mall:property:edit")
    @Log(title = "商品属性", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(MallProperty mallProperty)
    {
        return toAjax(mallPropertyService.updateMallProperty(mallProperty));
    }

    /**
     * 删除商品属性
     */
    @RequiresPermissions("mall:property:remove")
    @Log(title = "商品属性", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(mallPropertyService.deleteMallPropertyByIds(ids));
    }
}
